package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import entity.Goods;
import entity.ShopCar;
import services.ShopCarService;
import services.impl.ShopCarServiceImpl;


@WebServlet("/mycar")
public class MyCarServlet extends HttpServlet {

	ShopCarService shopService = new ShopCarServiceImpl();
	Gson gson = new Gson();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		int user_id = (int) session.getAttribute("uid");//获取用户id
		System.out.println(user_id);
		List<Goods> carGoodsIdList = null;
		if(user_id!=0) {
			carGoodsIdList = shopService.showCarGoods(user_id);
			if(carGoodsIdList!=null) {
			String carlist = gson.toJson(carGoodsIdList);
			out.print(carlist);
			}else {
				out.print(carGoodsIdList);
			}
		}else {
			out.print(carGoodsIdList);
		}
		
		out.flush();
		out.close();
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
